package com.platform.controller.backend;


import com.platform.base.BaseController;
import com.platform.common.Const;
import com.platform.common.ServerResponse;
import com.platform.pojo.User;
import com.platform.util.StringUtil;
import com.platform.vo.UserToken;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户控制层
 */
@RestController 
@RequestMapping("/userController/")
@CrossOrigin(origins = Const.REQUESTURL,maxAge = 3600,allowCredentials = "true")
public class UserController extends BaseController {


    /**
     * 登录
     * @param request
     * @return
     */
    @PostMapping(value = "login.do")
    public ServerResponse<UserToken> login(@RequestParam(value = "username") String username,
                                           @RequestParam(value = "password") String password,
                                           @RequestParam(value = "vcode") String vcode, HttpServletRequest request){
        //判断用户名是否在同一session领域登录
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if( o!= null){
            User u = (User)o;
            if(null != u){
                if(username.equals(u.getUsername())){
                    return ServerResponse.createBySuccessMessage("用户已登录");
                }
            }
        }
        //验证Code
        String SessionVCode = StringUtil.valueOfString(request.getSession().getAttribute(Const.CodeType.PCLOGINSTRCODE));
        if(StringUtil.isEmpty(vcode)){
            return ServerResponse.createByErrorMessage("验证码不能为空");
        }
        if(!vcode.toLowerCase().equals(SessionVCode.toLowerCase())){
            return ServerResponse.createByErrorMessage("验证码不正确！");
        }
        ServerResponse<UserToken> response = iUserService.login(username,password);
        if(response.isSuccess()){
            request.getSession().setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @PostMapping(value = "logout.do")
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    /**
     * 注册
     * @param param
     * @return
     */
    @PostMapping(value = "register.do")
    public ServerResponse<String> register(@RequestParam Map param,HttpServletRequest request){
        return iUserService.register(param,request);
    }

    /**
     * 更新用户信息
     * @param session
     * @param userMap
     * @return
     */
    @PostMapping(value = "update_information.do")
    public ServerResponse<User> update_information(HttpSession session, @RequestParam Map userMap){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        userMap.put("id",currentUser.getId());
        userMap.put("username",currentUser.getUsername());
        ServerResponse<User> response = iUserService.updateInformation(userMap);
        if(response.isSuccess()){
            response.getData().setUsername(currentUser.getUsername());
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    /**
     * 忘记密码修改密码发送验证码
     * @param param
     * @param request
     * @return
     */
    @PostMapping(value = "update_password_sendmessage")
    public ServerResponse<String> updatePasswordSendMessageCode(@RequestParam Map param, HttpServletRequest request){
        return iUserService.updatePasswordSendMessageCode(param,request);
    }

    /**
     * 用户修改密码
     * @return
     */
    @PostMapping(value = "forget_reset_password")
    public ServerResponse<String> forgetRestPassword(@RequestParam Map param, HttpServletRequest request){
        return iUserService.forgetRestPassword(param,request);
    }


    @GetMapping(value = "select_userinfo.do")
    public ServerResponse<Map> selectUserInfoByUsername(@RequestParam(value = "username") String username,
                                                        @RequestParam(value = "userid") String userid,
                                                        @RequestParam(value = "password") String password,
                                                        HttpServletRequest request){
        return null;
    }

    /**
     * 获取用户session
     * @param request
     * @return
     */
    @GetMapping(value = "getUserDataSession.do")
    public ServerResponse<User> getUserDataSession(HttpServletRequest request){
        Object o = request.getSession().getAttribute(Const.CURRENT_USER);
        if(null != o){
            User user  = (User)o;
            return ServerResponse.createBySuccess("获取成功",user);
        }
        return ServerResponse.createByErrorMessage("获取失败");
    }

    public ServerResponse<List<Map>> selectByUserRole(@RequestParam(value = "role",defaultValue = "U") String role,
                                                      @RequestParam(value = "displayName") String displayName){
        return null;
    }

    /**
     * @param phoneNumber
     * 注册验证码
     */
    @GetMapping(value = "registerSendMessage.do")
    public ServerResponse<String> registerSendMessage(@RequestParam(value = "phoneNumber") String phoneNumber,HttpServletRequest request){
        if(StringUtil.isEmpty(phoneNumber)){
            return ServerResponse.createByErrorMessage("手机号码不能为空");
        }
        Map map = new HashMap();
        map.put("phoneNumber",phoneNumber);
        iSendMessageSeivice.sendMessage(map,request);
        return ServerResponse.createBySuccessMessage("发送成功");
    }

    /**
     * 登录修改密码
     * @param paramMap
     * @param request
     * @return
     */
    @PostMapping("loginUpdatePassword.do")
    public ServerResponse<String> loginUpdatePassword(@RequestParam Map paramMap,HttpServletRequest request){
        return iUserService.loginUpdatePassword(paramMap,request);
    }
}
